import Lexer as L
import Parser as P
import Semantic as S
import json

def main_handler(event, context):
    code = event['body']
    code_path = '/tmp/code'
    f = open(code_path, 'w')
    f.write(code)
    f.close()

    msg = 'success'
    error_code = 0
    result = {}

    lexer = L.Lexer(code_path)
    sem = S.Semantic(lexer)
    sem.init()
    try:
        sem.parser()
        result = sem.getResult()
    except Exception as err:
        error_code = 1
        msg = str(err)
    print(result)
    return {
            "statusCode": 200,
            "headers": {"Content-Type": "text", "Access-Control-Allow-Origin": "*"},
            "body": json.dumps({'result':result, 'error_code': error_code, 'msg': msg})
        }


